package xin.qixia.builder.utils;

import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;

import java.util.ArrayList;
import java.util.List;

/**
 * TableBuilder工具类
 */
public class PdfTableBuilder {
    private final Font font;
    private final int columns;
    private final List<List<Cell>> grid = new ArrayList<>();
    private final List<MergeRegion> mergeRegions = new ArrayList<>();

    public PdfTableBuilder(int columns, Font font) {
        this.font = font;
        this.columns = columns;
    }

    public void addRow(List<Object> cellContents) {
        if (cellContents.size() != columns) {
            throw new IllegalArgumentException("单元格数量必须等于列数: " + columns);
        }

        List<Cell> row = new ArrayList<>();
        for (Object content : cellContents) {
            row.add(new Cell(content));
        }
        grid.add(row);
    }

    public void merge(int startRow, int startCol, int endRow, int endCol) {
        mergeRegions.add(new MergeRegion(startRow - 1, startCol - 1, endRow - 1, endCol - 1));
    }

    public PdfPTable build() {
        // 应用所有合并区域
        for (MergeRegion region : mergeRegions) {
            applyMerge(region);
        }

        // 创建PDF表格
        PdfPTable table = new PdfPTable(columns);
        for (List<Cell> row : grid) {
            for (Cell cell : row) {
                // 只添加未合并的单元格
                if (!cell.isMerged) {
                    PdfPCell pdfCell;
                    if (cell.content instanceof PdfPCell) {
                        pdfCell = (PdfPCell) cell.content;
                    } else {
                        pdfCell = new PdfPCell(new Paragraph(String.valueOf(cell.content), font));
                    }
                    if (cell.rowspan > 1 || cell.colspan > 1) {
                        pdfCell.setRowspan(cell.rowspan);
                        pdfCell.setColspan(cell.colspan);
                    }
                    table.addCell(pdfCell);
                }
            }
        }
        return table;
    }

    private void applyMerge(MergeRegion region) {
        int rowspan = region.endRow - region.startRow + 1;
        int colspan = region.endCol - region.startCol + 1;

        // 设置主单元格合并属性
        Cell mainCell = grid.get(region.startRow).get(region.startCol);
        mainCell.rowspan = rowspan;
        mainCell.colspan = colspan;

        // 标记被合并的单元格
        for (int r = region.startRow; r <= region.endRow; r++) {
            for (int c = region.startCol; c <= region.endCol; c++) {
                if (r != region.startRow || c != region.startCol) {
                    grid.get(r).get(c).isMerged = true;
                }
            }
        }
    }

    private static class Cell {
        Object content;
        int rowspan = 1;
        int colspan = 1;
        boolean isMerged = false;

        Cell(Object content) {
            this.content = content;
        }
    }

    private record MergeRegion(int startRow, int startCol, int endRow, int endCol) {
    }
}
